package com.aidong.liveVideo.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.liveVideo.biz.TrnLiveVideoBiz;
import com.aidong.liveVideo.entity.LiveVideoMoreList;
import com.aidong.liveVideo.entity.TrnLiveVideo;
import com.aidong.liveVideo.entity.TrnLiveVideoList;
import com.aidong.util.ReadPropertiesUtil;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class TrnLiveVideoAction implements ModelDriven<TrnLiveVideo> {
	private TrnLiveVideo trnLiveVideo;
	
	@Override
	public TrnLiveVideo getModel() {
		if (trnLiveVideo == null) {
			trnLiveVideo = new TrnLiveVideo();
		}
		return trnLiveVideo;
	}
	private Map<String, Object> map;

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	private TrnLiveVideoBiz trnLiveVideoBiz; 
	@Autowired
	public void setTrnLiveVideoBiz(TrnLiveVideoBiz trnLiveVideoBiz) {
		this.trnLiveVideoBiz = trnLiveVideoBiz;
	} 
	
	
	/**
	 * 直播列表
	 * 
	 * @return
	 */
	@Action(value = "getLiveVideoList", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getLiveVideoList() {
		map = new HashMap<String, Object>();
//		try{
			//Map<String, Object> pageMap = PageUtil.page(20, trnLiveVideo.getPageCurrent());
			Map<String, Object> pageMap =new HashMap<String,Object>();
			Calendar calendar = Calendar.getInstance();
			Date date=calendar.getTime();
			Date now=new Date();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			pageMap.put("time", sf.format(date));
			//获取正在直播信息
			List<TrnLiveVideoList> liveVideo=trnLiveVideoBiz.getLiveVideoList(pageMap);
			List<TrnLiveVideoList> livePersion=trnLiveVideoBiz.getLivePersionCount(pageMap);
			for(int i=0;i<liveVideo.size();i++){
				try{
					liveVideo.get(i).setType(1);
					liveVideo.get(i).setPersonCou(livePersion.get(i).getPersonCou());
					
				}catch(Exception e){
					e.printStackTrace();
				}
				if(liveVideo.get(i).getIsLiveShow()==0){
					
					now=new Date();
					Date begin = null;
					try {
						begin = sf.parse(liveVideo.get(i).getLiveBeginTime());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					if((now.getTime()-begin.getTime())/1000 >= 0 && (now.getTime()-begin.getTime())/1000 <= 90){
						liveVideo.get(i).setLiveCover(ReadPropertiesUtil.getLiveVideoCover(liveVideo.get(i).getLiveCover()));
					}else{
						liveVideo.get(i).setLiveCover(liveVideo.get(i).getLivePath().replaceFirst("rtmp", "http").replace("-rtmp", "-snapshot")+".jpg");
					}
				}else{
					liveVideo.get(i).setLivePath(ReadPropertiesUtil.getVideo(liveVideo.get(i).getVideoPath()));
					liveVideo.get(i).setLiveCover(ReadPropertiesUtil.getLiveVideoCover(liveVideo.get(i).getLiveCover()));
				}
			}
			TrnLiveVideoList liveHome=trnLiveVideoBiz.getLiveHomeList();
			//获取视频分类
			//List<TrnVideoLimit> videoLimit=trnLiveVideoBiz.getVideoLimitList();
			//除正在直播以外的今天以后的直播
			List<LiveVideoMoreList> liveVideoMore=trnLiveVideoBiz.getLiveVideoMoreList(pageMap);
			for(int i=0;i<liveVideoMore.size();i++){
				List<TrnLiveVideoList> li=liveVideoMore.get(i).getLiveVideoList();
				for(int t=0;t<li.size();t++){
					if(li.get(t).getIsLiveShow()==0){
						//li.get(i).setLiveCover(li.get(i).getLivePath().replaceFirst("rtmp", "http").replace("-rtmp", "-snapshot")+".jpg");
					}else{
						li.get(t).setLivePath(ReadPropertiesUtil.getVideo(li.get(t).getVideoPath()));
						//li.get(i).setLiveCover(ReadPropertiesUtil.getLiveVideoCover(li.get(i).getLiveCover()));
					}
				}
			}
			
			Map<String,Object> resultMap=new HashMap<String,Object>();
			resultMap.put("liveVideo", liveVideo);
			//resultMap.put("video", videoLimit);
			resultMap.put("liveVideoMore", liveVideoMore);
			resultMap.put("liveHome", liveHome);
			map.put("code", 1);
			map.put("result", resultMap);
			return "succ";
//		} catch(Exception e) {
//			e.printStackTrace();
//			map.put("code", 0);
//			map.put("msg", "系统异常");
//			return "succ";
//		}
		
	}
	
	public static void main(String[] args){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now=new Date();
		try {
			Date begin=sf.parse("2016-12-13 16:47:00");
			System.out.println((now.getTime()-begin.getTime())/1000);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	/**
	 * 单个视频详情
	 * 
	 * @return
	 */
	@Action(value = "getLiveVideoDetail", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getVideoDetail() {
		map = new HashMap<String, Object>();
		if(trnLiveVideo.getLiveId() > 0){
			try{
				Map<String, Object> pageMap =new HashMap<String,Object>();
				pageMap.put("liveId", trnLiveVideo.getLiveId());
				TrnLiveVideoList list=trnLiveVideoBiz.getLiveVideoDetail(pageMap);
				TrnLiveVideoList detail=trnLiveVideoBiz.getLiveVideoLike(pageMap);
				
				if(list.getIsLiveShow()==0){
					
				}else{
					list.setLivePath(ReadPropertiesUtil.getVideo(list.getVideoPath()));
					//li.get(i).setLiveCover(ReadPropertiesUtil.getLiveVideoCover(li.get(i).getLiveCover()));
				}
				
				list.setPraiseCou(detail.getPraiseCou());
				list.setCommentsCou(detail.getCommentsCou());
				Map<String,Object> resultMap=new HashMap<String,Object>();
				resultMap.put("liveDetail", list);
				//resultMap.put("livePersion", trnLiveVideoBiz.getLivePersionList(pageMap));
				map.put("code", 1);
				map.put("result", resultMap);
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	/**
	 *正在直播记录人员
	 * 
	 * @return
	 */
	@Action(value = "addLivingMsg", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String addLivingMsg() {
		map = new HashMap<String, Object>();
		if(trnLiveVideo.getLiveId() > 0){
			try{
				Map<String, Object> pageMap =new HashMap<String,Object>();
				pageMap.put("liveId", trnLiveVideo.getLiveId());
				pageMap.put("idongId", trnLiveVideo.getIdongId());
				trnLiveVideoBiz.addLiveVideoPersion(pageMap);
				map.put("code", 1);
				map.put("result", "添加成功");
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	/**
	 * 正在直播的主播及人数
	 * @return
	 */
	@Action(value = "getLivingPersion", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getLivingPersion() {
		map = new HashMap<String, Object>();
		if(trnLiveVideo.getLiveId() > 0){
			try{
				Map<String, Object> pageMap =new HashMap<String,Object>();
				pageMap.put("liveId", trnLiveVideo.getLiveId());
				map.put("code", 1);
				map.put("result", trnLiveVideoBiz.getLivePersion(pageMap));
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
}
